unit Unit52; 

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, Menus,
  sqldb, db, DBGrids, Unit1, Unit53, Unit54, Unit58;

type

  { TForm52 }

  TForm52 = class(TForm)
    Datasource1: TDatasource;
    DBGrid1: TDBGrid;
    MainMenu1: TMainMenu;
    MenuItem1: TMenuItem;
    MenuItem10: TMenuItem;
    MenuItem2: TMenuItem;
    MenuItem3: TMenuItem;
    MenuItem4: TMenuItem;
    MenuItem5: TMenuItem;
    MenuItem6: TMenuItem;
    MenuItem7: TMenuItem;
    MenuItem8: TMenuItem;
    MenuItem9: TMenuItem;
    SQLQuery1: TSQLQuery;
    procedure DBGrid1CellClick(Column: TColumn);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure FormShow();
    procedure MenuItem10Click(Sender: TObject);
    procedure MenuItem4Click(Sender: TObject);
    procedure MenuItem5Click(Sender: TObject);
    procedure MenuItem6Click(Sender: TObject);
    procedure MenuItem8Click(Sender: TObject);
    procedure MenuItem9Click(Sender: TObject);
    procedure ordenar_grid();
  private
    nombre : string;
    tipo : string;
  public
    { public declarations }
  end; 

var
  Form52: TForm52; 

implementation

{ TForm52 }

procedure TForm52.ordenar_grid();
begin
  if SQLQuery1.Active = True then
    SQLQuery1.Close;

  SQLQuery1.SQL.Text := 'select * from sucursal where suc_nombre ilike ''%' + nombre + '%'''
    + ' and suc_descripcion ilike ''' + tipo + '''';

  SQLQuery1.Open;

  DBGrid1.Columns[0].Title.Caption := 'Código';
  DBGrid1.Columns[1].Title.Caption := 'Nombre';
  DBGrid1.Columns[2].Title.Caption := 'Tipo';


  DBGrid1.Columns[0].Width := 60;
  DBGrid1.Columns[1].Width := 160;
  DBGrid1.Columns[2].Width := 200;
end;

procedure TForm52.FormCreate(Sender: TObject);
begin
  SQLQuery1.DataBase := Form1.PQConnection1;
  SQLQuery1.Transaction := Form1.SQLTransaction1;
end;

procedure TForm52.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  if SQLQuery1.Active = True then
    SQLQuery1.Close;
end;

procedure TForm52.DBGrid1CellClick(Column: TColumn);
begin
  Form54.Edit1.Text := SQLQuery1.FieldByName('suc_cdg').AsString;
  Form54.Edit2.Text := SQLQuery1.FieldByName('suc_nombre').AsString;
  Form54.ComboBox1.Text := SQLQuery1.FieldByName('suc_descripcion').AsString;
  
  Form54.ShowModal;
  
  ordenar_grid();
end;

procedure TForm52.FormShow();
begin
  nombre := '%';
  tipo := '%';
  
  ordenar_grid();
end;

procedure TForm52.MenuItem10Click(Sender: TObject);
begin
  Form58.ShowModal;
  tipo := Form58.ComboBox1.Text;

  ordenar_grid;
end;

procedure TForm52.MenuItem4Click(Sender: TObject);
begin
  Form53.ShowModal;
  
  ordenar_grid();
end;

procedure TForm52.MenuItem5Click(Sender: TObject);
var
  codigo : string;
  QueryString : string;
  
begin
  codigo := InputBox('Baja de Sucursal', 'Ingrese el código de sucursal a eliminar:', '');

  if codigo <> '' then
  begin
    QueryString := 'delete from sucursal where suc_cdg = ' + codigo;

    Form1.PQConnection1.ExecuteDirect('begin work;');
    Form1.PQConnection1.ExecuteDirect(QueryString);
    Form1.PQConnection1.ExecuteDirect('commit work;');

    ShowMessage('BAJA EXITOSA');

    ordenar_grid();
  end;
end;

procedure TForm52.MenuItem6Click(Sender: TObject);
begin
  ShowMessage('Click en el punto de venta a modificar...');
end;

procedure TForm52.MenuItem8Click(Sender: TObject);
begin
  nombre := InputBox('Filtro por Nombre', 'Ingrese parte del nombre de la sucursal' , '');
  
  ordenar_grid;
end;

procedure TForm52.MenuItem9Click(Sender: TObject);
begin
  FormShow;
end;

initialization
  {$I unit52.lrs}

end.

